package service

import (
	"organoid_public/domain/fitness/enum"
	"organoid_public/domain/fitness/model"
	"organoid_public/domain/fitness/respository"
	"organoid_public/infrastructure/db"
	"organoid_public/infrastructure/tool"
	"organoid_public/pkg/alipay"
	"organoid_public/pkg/alipay/dto"
	"organoid_public/pkg/alipay/entity"
)

type MerchantServiceD struct {
	repo      respository.MerchantRepoInterface
	storeRepo respository.StoreRepoInterface
}

type SaveOrInsertParams struct {
	OpenAppId      string `json:"open_app_id" `
	OpenMerchantId string `json:"open_merchant_id" `
	MerchantName   string `json:"merchant_name" `
	Appid          string `json:"appid" `
	Phone          string `json:"phone" `
	ShopId         string `json:"shop_id" `
	MerchantPid    string `json:"merchant_pid" `
	AppAuthToken   string `json:"app_auth_token"`
	ReqCommitAli   int    `json:"req_commit_ali"`
}

func NewMerchantServiceD(repo respository.MerchantRepoInterface) MerchantServiceInterface {
	return &MerchantServiceD{repo: repo}
}

func (m *MerchantServiceD) UpLogoAndSave() bool {
	return false
}

func (m *MerchantServiceD) Lists() []*model.Merchant {
	return nil
}
func (m *MerchantServiceD) Detail(id int) *model.Merchant {

	return m.repo.GetMerchantInfoById(id)
}
func (m *MerchantServiceD) SaveOrInsert(params *SaveOrInsertParams) *model.Merchant {
	//判断是否是编辑
	var merchantInfo = &model.Merchant{}
	if len(params.OpenMerchantId) > 0 {
		merchantInfo = m.repo.GetMerchantInfoByOpenMerchantId(params.OpenMerchantId)
		if merchantInfo.Id <= 0 {
			panic("未获取到商户信息")
		}
	}
	//提交支付
	if params.ReqCommitAli == 1 {
		aop := alipay.NewAop()
		merchantEntity := entity.AlipayCommerceMerchantcardOpenCreateEntity{}
		PriceInfos := entity.PriceInfosType{
			PriceRate:       "0.06",
			BackProductCode: "I1140100001000001000",
		}
		SettleInMerchantLicense := entity.SettleInMerchantLicenseType{
			Phone: params.Phone,
			Name:  params.MerchantName,
		}
		merchantEntity.MerchantName = params.MerchantName
		merchantEntity.Pid = params.MerchantPid
		merchantEntity.CardTypes = []string{"AXF_MERCHANT_PERIOD_PAY"}
		merchantEntity.NeedAuth = true
		merchantEntity.PriceInfos = []*entity.PriceInfosType{&PriceInfos}
		merchantEntity.SettleInMerchantLicense = &SettleInMerchantLicense
		result := new(dto.AlipayCommerceMerchantcardOpenCreateDto)
		aop.Execute(&merchantEntity, result)
		if result.Error.Code != "10000" {

			panic(result.Error.SubMsg)
		}
	}
	var MerchantStatus string
	if params.ReqCommitAli == 1 {
		MerchantStatus = enum.STATUS_RECOVERING
	} else {
		MerchantStatus = enum.STATUS_NORMAL
	}
	if merchantInfo.Id > 0 {
		var Base = db.NewBaseRepo()
		Base.AddUpdate(params)
		Base.AddWhere("id", merchantInfo.Id)
		delete(Base.UpdateData, "req_commit_ali")
		err := m.repo.UpdateMerchant(Base)
		if err != nil {
			panic(err)
		}
	} else {
		merchant := &model.Merchant{
			Status:            MerchantStatus,
			MerchantName:      params.MerchantName,
			MerchantPid:       params.MerchantPid,
			Phone:             params.Phone,
			MerchantLoginName: params.MerchantName,
			ShopId:            params.ShopId,
			Appid:             params.Appid,
			OpenMerchantId:    tool.RandStr(10),
			OpenAppId:         params.OpenAppId,
			AppAuthToken:      params.AppAuthToken,
		}
		err := m.repo.InsertMerchant(merchant)
		if err != nil {
			panic(err)
		}
	}
	return nil
}

func (m *MerchantServiceD) BindStore(params interface{}) error {
	paramsMap := tool.StructToMap(params)
	id, exit := paramsMap["id"]
	if exit && (id.(int) > 0) {
		var Base = db.NewBaseRepo()
		Base.AddWhere("id", id)
		Base.AddUpdate(params)
		err := m.storeRepo.UpdateStore(Base)
		if err != nil {
			panic(err)
		}
	} else {
		err := m.storeRepo.InsertStore(paramsMap)
		if err != nil {
			panic(err)
		}
	}
	return nil
}
